//package com.ruoyi.platform.config;
//
//import com.ruoyi.platform.config.properties.PermitAllUrlProperties;
//import com.ruoyi.platform.security.filter.JwtAuthenticationTokenFilter;
//import com.ruoyi.platform.security.handle.AuthenticationEntryPointImpl;
//import com.ruoyi.platform.security.handle.LogoutSuccessHandlerImpl;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpMethod;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
//import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.http.SessionCreationPolicy;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.web.SecurityFilterChain;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.springframework.security.web.authentication.logout.LogoutFilter;
//import org.springframework.web.cors.CorsConfiguration;
//import org.springframework.web.cors.CorsConfigurationSource;
//import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
//import org.springframework.web.filter.CorsFilter;
//
//@Configuration
//@EnableWebSecurity	// 添加 security 过滤器
//@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
//public class SpringSecurityConfig {
//
//    /**
//     * 自定义用户认证逻辑
//     */
//    @Autowired
//    private UserDetailsService userDetailsService;
//
//    /**
//     * 认证失败处理类
//     */
//    @Autowired
//    private AuthenticationEntryPointImpl unauthorizedHandler;
//
//    /**
//     * 退出处理类
//     */
//    @Autowired
//    private LogoutSuccessHandlerImpl logoutSuccessHandler;
//
//    /**
//     * token认证过滤器
//     */
//    @Autowired
//    private JwtAuthenticationTokenFilter authenticationTokenFilter;
//
//    /**
//     * 跨域过滤器
//     */
//    @Autowired
//    private CorsFilter corsFilter;
//
//    /**
//     * 允许匿名访问的地址
//     */
//    @Autowired
//    private PermitAllUrlProperties permitAllUrl;
//
//    /**
//     * 强散列哈希加密实现
//     */
//    @Bean
//    public BCryptPasswordEncoder bCryptPasswordEncoder()
//    {
//        return new BCryptPasswordEncoder();
//    }
//
//    /**
//     * 获取AuthenticationManager（认证管理器），登录时认证使用
//     * @param authenticationConfiguration
//     * @return
//     * @throws Exception
//     */
//    @Bean
//    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
//        return authenticationConfiguration.getAuthenticationManager();
//    }
//
//    @Bean
//    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//        return http
//                // 基于 token，不需要 csrf
//                .csrf().disable()
//                // 禁用HTTP响应标头
//                .headers().cacheControl().disable().and()
//                // 认证失败处理类
//                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
//                // 基于 token，不需要 session
//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
//                // 下面开始设置权限
//                .authorizeRequests(authorize -> authorize
//                        // 请求放开
//                        .antMatchers("/login","pLogin/getInfo", "/register", "/captchaImage").permitAll()
//                        // 静态资源，可匿名访问
//                        .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
//                        .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
//                        // 其他地址的访问均需验证权限
//                        .anyRequest().authenticated()
//                )
//                // 添加 JWT 过滤器，JWT 过滤器在用户名密码认证过滤器之前
//                .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
//                // 添加CORS filter
//                .addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class)
//                .addFilterBefore(corsFilter, LogoutFilter.class).logout()
//                .build();
//    }
//
//    /**
//     * 配置跨源访问(CORS)
//     * @return
//     */
//    @Bean
//    CorsConfigurationSource corsConfigurationSource() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
//        return source;
//    }
//
//}
